<template>
    <div class="list">
     <!--头部面包屑导航-->
         <el-breadcrumb separator-class="el-icon-arrow-right" >
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
    <!-- 卡片视图 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="7">
                    <el-input clearable @clear="getgoodslist" placeholder="请输入内容" v-model="queryinfo.query" class="input-with-select">
                        <el-button @click="searchlist" slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goaddgoods"> 添加商品 </el-button>
                </el-col>
            </el-row>
            <!-- table 表格-->
            <el-table :data="goodslist" border stripe>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width="110px"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量(kg)" width="120px"></el-table-column>
                <el-table-column prop="add_time" label="商品创建时间" width="240px">
                    <template slot-scope="scope">
                        {{scope.row.add_time|dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column  label="操作" width="140px">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" class="el-icon-edit" ></el-button>
                        <el-button type="danger" size="mini" class="el-icon-delete" @click="deletebtn(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页器 -->
            <el-pagination
                @size-change="goodslisthandleSizeChange"
                @current-change="goodslisthandleCurrentChange"
                :current-page="queryinfo.pagenum"
                :page-sizes="[6, 8, 10, 12]"
                :page-size="queryinfo.pagesize"
                 layout="total, sizes, prev, pager, next, jumper"
                :total="400">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
export default {
    data() {
        return {
            queryinfo:{//参数数据
                query:"",
                pagenum:1,
                pagesize:10
            },
            goodslist:[],//商品列表数据
            total:0,//商品数据条数
        }
    },
    created(){
        //请求商品数据
        this.getgoodslist()
    },
    methods: {
        //请求商品数据
        async getgoodslist(){
            const {data:res} =  await this.$http.get('goods',{
                params:this.queryinfo
            })
            if(res.meta.status!=200) return this.$message.error("获取商品列表数据失败")
            this.goodslist = res.data.goods
            this.total =res.data.total
            console.log(res)
        },
        //每页天数变化
        goodslisthandleSizeChange(e){
            this.queryinfo.pagesize = e
            this.getgoodslist()
        },
        //页数变化
        goodslisthandleCurrentChange(e){
            this.queryinfo.pagenum = e
            this.getgoodslist()
        },
        //搜索
        searchlist(){
            this.getgoodslist()
        },
        //删除商品
        async deletebtn(row){
            const resinfo = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).catch(err=>err)
            if(resinfo!="confirm") return this.$message.info("您取消了本次删除")
            const {data:res} =  await this.$http.delete('goods/'+row.goods_id)
            if(res.meta.status!=200) return this.$message.error("删除该商品数据失败")
            this.getgoodslist()
            return this.$message.success("成功删除该商品")
        },
        //去添加商品页面
        goaddgoods(){
            this.$router.push('/goods/add')
        }
    },
}
</script>

<style scoped>
.el-table{
    margin-top: 10px;
}
</style>